// pages/tui/tui.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    left: 0,
    top: 0,
    array1:['仅退款','退货退款'],
    index1:0,
    array2: ['点击选择申请原因', '多拍、错拍、不想要', '不喜欢、效果不好', '货物与描述不符', '质量问题', '收到商品少件、破损、污渍等', '卖家发错货', '假冒品牌', '其他原因'],
    index2: 0,
    imgs:[],
    orderid:0,
    money:0,
    mobile:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    that.setData({ orderid: parseInt(options.orderid), orderdetailid: parseInt(options.orderdetailid), productid: parseInt(options.productid), money: parseFloat(options.money), mobile:options.mobile });
    wx.getSystemInfo({
      success: function (res) {
        if (res.platform == "ios") {
          that.setData({ left: -10, top: -20 });
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  openType: function(e){
    var index1=e.detail.value;
    this.setData({index1:index1});
  },
  openReason: function (e) {
    var index2 = e.detail.value;
    this.setData({ index2: index2 });
  },
  chooseImage: function (e) {
    var that = this;
    var imgs = this.data.imgs;
    wx.chooseImage({
      count: 3, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        for (var i = 0; i < res.tempFilePaths.length; i++) {
          imgs.push(res.tempFilePaths[i]);
        }
        that.setData({ imgs: imgs });
      }
    })
  },
  formSubmit: function (e) {
    var that = this;
    var error = false;
    var imgcount = that.data.imgs.length;
    var uploadcount = 0;
    var data = {};
    data.orderid=that.data.orderid;
    data.orderdetailid = that.data.orderdetailid;
    data.productid = that.data.productid;
    data.type = that.data.array1[that.data.index1];
    data.reason = that.data.array2[that.data.index2];
    data.money=e.detail.value.money;
    data.context = e.detail.value.context;
    data.mobile = e.detail.value.mobile;
    if (this.data.index2==0) {
      app.showError("请选择申请原因");
      return;
    }
    if (data.money.trim()=="" || data.money=="0"){
      app.showError("请输入退款金额");
      return;
    }
    if (data.context.trim() == "") {
      app.showError("请输入申请说明");
      return;
    }
    if (data.mobile.trim() == "") {
      app.showError("请输入联系电话");
      return;
    }
    if (this.data.imgs.length==0) {
      app.showError("请上传凭证");
      return;
    }
    data.act = "Save_Tui";
    wx.showLoading({ title: '正在提交...' });
    app.Ajax(data, function (json) {
      if (json.state) {
        var imgs = that.data.imgs;
        for (var m = 0; m < imgs.length; m++) {
        wx.uploadFile({
          url: 'https://shop.qiyedaishu.com/tools/Upload.ashx', //仅为示例，非真实的接口地址
          filePath: imgs[m],
          name: 'img',
          formData: {
            'handle': 'upload_file_a',
            'sid': json.data,
            'tablename': 'tui',
            'userid': 1
          },
          success: function (res) {
            var data = res.data
            uploadcount++;
            if (uploadcount == imgcount) {
              console.log(res);
              wx.hideLoading();
              app.showSuccess("提交成功");
              var pages = getCurrentPages();
              var prevPage = pages[pages.length - 2];
              prevPage.setData({ refresh: true });
              setTimeout(function () {
                wx.navigateBack();
              }, 1000);
            }
          }
        });
        }
      }
    });
  }
})